package com.eascs.web.o2o.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.web.dao.BidmDao;
import com.eascs.web.o2o.web.form.ChannelBusStatisForm;
import com.eascs.web.o2o.web.form.RecommendStaticForm;

@Service
public class RecommendStaticService {

	
	@Autowired
	BidmDao bidmDao;
	
	
	public PageResult<Object[]> getRecommendStaticByPage(ChannelBusStatisForm entity){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(bidmDao.getSql("sql.RecommendStaticService.getRecommendStaticByPage")); 
		if(!StringUtil.isEmpty(entity.getBillCode())){
			querySql.append(" AND t.bill_code = ?");
			sqlPageQuery.addParam(entity.getBillCode());
		}
		if(!StringUtil.isEmpty(entity.getLoanType())){
			querySql.append(" and t.loan_type_code in ( ");
			String loanTypeA [] = {};  
			String loanType = "";
			StringBuffer loanTypes = new StringBuffer();
			if(entity.getLoanType().contains(",")){
				loanTypeA = entity.getLoanType().split(",");
				for (int i = 0; i < loanTypeA.length; i++) {
					loanTypes.append("'" + loanTypeA[i] + "',");
				}
			}else{
				loanTypes.append("'"+entity.getLoanType()+"',");
			}
			loanType = loanTypes.toString().substring(0,loanTypes.toString().length()-1);
			querySql.append(loanType);
			querySql.append(")");
		}
		if(!StringUtil.isEmpty(entity.getPartnerType())){
			querySql.append(" and t.partner_type_code in (");
			String loanTypeA [] = {};  
			String loanType = "";
			StringBuffer loanTypes = new StringBuffer();
			if(entity.getPartnerType().contains(",")){
				loanTypeA = entity.getPartnerType().split(",");
				for (int i = 0; i < loanTypeA.length; i++) {
					loanTypes.append("'" + loanTypeA[i] + "',");
				}
			}else{
				loanTypes.append("'"+entity.getPartnerType()+"',");
			}
			loanType = loanTypes.toString().substring(0,loanTypes.toString().length()-1);
			querySql.append(loanType);
			querySql.append(")");
		}
		if(!StringUtil.isEmpty(entity.getLoanerName())){
			querySql.append(" AND t.loaner_name LIKE ?");
			sqlPageQuery.addLikeParam(entity.getLoanerName());
		}
		if(!StringUtil.isEmpty(entity.getPartnerName())){
			querySql.append(" AND t.partner_name = ?");
			sqlPageQuery.addParam(entity.getPartnerName());
		}
		if(!StringUtil.isEmpty(entity.getLoanDate())){
			querySql.append(" AND t.loan_date >= ?");
			sqlPageQuery.addParam(entity.getLoanDate());
		}
		if(!StringUtil.isEmpty(entity.getLoanDateEnd())){
			querySql.append(" AND t.loan_date <= ?");
			sqlPageQuery.addParam(entity.getLoanDateEnd());
		}
		querySql.append("  ORDER BY t.loan_date DESC ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return bidmDao.findDataWithPage(sqlPageQuery);
	}
}
